package view.matricula;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;

import model.modalidade.ModalidadeProfessor;

public class ModalityPanel extends JPanel implements ActionListener{
	
	final String nomes[] = {"Carlos", "Marcelo", "Fabiana",
		      "Carolina", "Osmar"};

	private JList<ModalidadeProfessor> jlistModality;
	private JList<ModalidadeProfessor> jlistModalitySelect;

	private JButton btPassar;
	private JButton btVoltar;
	
	private JPanel pnBotoes;
	
	private DefaultListModel modelo;
	private DefaultListModel modeloSelect;

	public ModalityPanel() {
		initialize();
		setupLayouts();
		layoutComponents();
	}

	private void initialize() {
		
		modelo = new DefaultListModel();
		modeloSelect = new DefaultListModel();
		
		int i = 0;
		for (String nome : nomes){
			modelo.addElement(nome);
		}
		
		jlistModality = new JList<ModalidadeProfessor>();
		jlistModalitySelect = new JList<ModalidadeProfessor>();
		
		jlistModality.setModel(modelo);
		jlistModality.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

		jlistModalitySelect.setModel(modeloSelect);
		jlistModalitySelect.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		
		pnBotoes = new JPanel();

		btPassar = new JButton(">");
		btVoltar = new JButton("<");
		
		btPassar.addActionListener(this);
		btPassar.setActionCommand("add");
		btVoltar.addActionListener(this);
		btVoltar.setActionCommand("remove");
		
	}
	
	private void setupLayouts(){
		pnBotoes.setLayout(new GridLayout(2, 1, 5, 5));
		this.setLayout(new FlowLayout());
	}

	private void layoutComponents() {
		pnBotoes.add(btPassar);
		pnBotoes.add(btVoltar);
		
		this.add(jlistModality);
		this.add(pnBotoes);
		this.add(jlistModalitySelect);
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
//		frame.pack();
		frame.setContentPane(new ModalityPanel());
		frame.setTitle("Teste Jlist");
		frame.setSize(500, 500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("add")){
			String [] valores = (String[]) jlistModality.getSelectedValues();
			for(int i = valores.length - 1; i >= 0; i--){
				((DefaultListModel)(jlistModalitySelect.getModel()))
				.addElement(valores[i]);
	            ((DefaultListModel)(jlistModality.getModel()))
	              .removeElement(valores[i]);
	          } 
			this.repaint();
		}
		if (e.getActionCommand().equals("remove")){
			
		}
	}

}
